var dayjs = require('dayjs');

Component({
    options:{
        styleIsolation:'isolated'
    },
    properties:{
        start:{
            type:String,
            value:""
        },
        end:{
            type:String,
            value:""
        },
        fields:{
            type:String,
            value:"day"
        }
    },
    data:{
        start:"开始日期",
        end:"结束日期",
        startDate:"",
        endDate:"",
        isRotateStart:false,
        isRotateEnd:false
    },
    methods: {
        open_start_select(){
            this.setData({
                isRotateStart:true
            })
        },
        open_end_select(){
            this.setData({
                isRotateEnd:true
            })
        },
        handle_start_confirm(e){
            this.setData({
                start:e.detail.value,
                startDate:e.detail.value,
                end:"结束日期",
                isRotateStart:false
            })
        },
        handle_cancel(){
            this.setData({
                isRotateStart:false,
                isRotateEnd:false
            })
        },
        handle_end_confirm(e){
            let start = this.data.start.replace(/-/g, '');
            let end = e.detail.value.replace(/-/g, '');

            if(this.data.fields == "day"){
                start = dayjs(start).format("YYYY-MM-DD")
                end = dayjs(end).format("YYYY-MM-DD");
            }

            if(this.data.fields == "month"){
                start = dayjs(start).startOf("month").format("YYYY-MM-DD")
                end = dayjs(end).endOf("month").format("YYYY-MM-DD");
            }

            if( end*1 < start*1 ){
                wx.showToast({
                    title: '开始日期大于结束日期',
                    icon: 'none',
                    image: '',
                    duration: 1500,
                    mask: true
                });
                this.setData({
                    isRotateEnd:false
                });
                return;
            }
            this.setData({
                end:e.detail.value,
                isRotateEnd:false
            });

            this.triggerEvent("confirm", new Array(start, end));
        },
        reset(){
            console.log( 'reset' )
            this.setData({
                start:"开始日期",
                end:"结束日期"
            })
        }
    },
})
